//
//  NearbyViewController.m
//  God_is_a_Girl
//
//  Created by 李继鹏 on 15/9/23.
//  Copyright (c) 2015年 呸. All rights reserved.
//

#import "NearbyViewController.h"
#import "NearbyModel.h"
#import <AFNetworking.h>
#import "NearbyTableViewCell.h"
#import <CoreLocation/CoreLocation.h>
#import "NearbyWebViewController.h"
@interface NearbyViewController ()<UITableViewDataSource, UITableViewDelegate, CLLocationManagerDelegate>

@property (nonatomic, retain)UITableView *tableview;

@property (nonatomic, retain)UIView *myView;

@property (nonatomic, assign)BOOL i;

@property (nonatomic, retain)NSMutableArray *ALLarr;

@property (nonatomic , strong)CLLocationManager *locationManager;

@property (nonatomic, retain)NSString *latitude;

@property (nonatomic, retain)NSString *longitude;

@property (nonatomic, retain)NSMutableArray *nextArr;

@end

@implementation NearbyViewController


- (void)viewWillAppear:(BOOL)animated
{
    _locationManager=[[CLLocationManager alloc] init];
    _locationManager.delegate=self;
    _locationManager.desiredAccuracy=kCLLocationAccuracyBest;
    _locationManager.distanceFilter=10;
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8) {
        [_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据（iOS8定位需要）
    }
    [_locationManager startUpdatingLocation];//开启定位
    
    
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{
    CLLocationCoordinate2D loc = [newLocation coordinate];
    
    _latitude = [NSString stringWithFormat: @"%f", loc.latitude];//获取纬度
    
    NSLog(@"1111111111.....%@", self.latitude);
    
    _longitude = [NSString stringWithFormat: @"%f", loc.longitude];//获取经度
    NSLog(@"2222222222.....%@", self.longitude);
    
    [self DataHandel];
    
}

//调用出现Error调用的方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    
    NSLog(@"11");
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(downButton: )];
    [self tabelViewSubView];
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 6 * 5, 0, self.view.frame.size.width / 6, 0)];
    self.myView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.myView];
    
    
}

- (void)DataHandel
{
    self.ALLarr = [[NSMutableArray alloc]init];
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?category=0&start=0&count=20&sort=default&shift=false&latitude=%@&longitude=%@", self.latitude, self.longitude];
    NSLog(@"%@",str); 
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *arr = [responseObject objectForKey:@"items"];
        NSLog(@"%@",arr);
        for (NSDictionary *dic in arr) {
            
            NearbyModel *model = [[NearbyModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ALLarr addObject:model];
        }
        NSLog(@"%ld", self.ALLarr.count);
        [self.tableview reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@", error);
        
    }];
    
    
}




- (void)tabelViewSubView
{
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableview.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:246 / 255.0 blue:233 / 255.0 alpha:1];
    [self.view addSubview:self.tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    return self.ALLarr.count;
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NearbyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    if (cell == nil) {
        cell = [[NearbyTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    cell.backgroundColor = [UIColor colorWithRed:255 / 255.0 green:246 / 255.0 blue:233 / 255.0 alpha:1];
    NearbyModel *model = [self.ALLarr objectAtIndex:indexPath.row];
    cell.model = model;
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 125;
}


- (void)downButton:(UIBarButtonItem *)button
{
    
    if (self.i == 1) {
        
        [UIView animateWithDuration:0.3 animations:^{
            
            self.myView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width / 6, 0);
            for (UIButton *button1 in self.myView.subviews) {
                [button1 removeFromSuperview];
            }
            
        }];
        self.i = 0;
        
        
    }
    else{
        
        [UIView animateWithDuration:0.5 animations:^{
            
            self.myView.frame = CGRectMake(self.view.frame.size.width / 6 * 5, 0, self.view.frame.size.width / 6, 240);
            NSArray * arr = @[@"全部",@"景点",@"住宿",@"餐厅",@"娱乐",@"购物"];
            for (int j = 0; j < 6; j++) {
                UIButton *downbuttons = [UIButton buttonWithType:UIButtonTypeCustom];
                downbuttons.frame = CGRectMake(0, j * 40, self.view.frame.size.width / 6, 40);
                downbuttons.backgroundColor = [UIColor lightGrayColor];
                [self.myView addSubview:downbuttons];
                [downbuttons setTitle:arr[j] forState:UIControlStateNormal];
                
                [downbuttons addTarget:self action:@selector(TZbutton:) forControlEvents:UIControlEventTouchUpInside];
                
                downbuttons.tag = 100+j;
            }
            
        }];
        self.i = 1;
        
    }
    
}

- (void)TZbutton:(UIButton *)button
{
    NSLog(@"%ld",button.tag);
    [UIView animateWithDuration:0.3 animations:^{
        
        self.myView.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width / 6, 0);
        for (UIButton *button1 in self.myView.subviews) {
            [button1 removeFromSuperview];
        }
        
    }];
    self.i = 0;
    
    NSArray *arr = @[@"0",@"11",@"10",@"5",@"21",@"6"];
    
    [self.ALLarr removeAllObjects];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"latitude=%@&longitude=%@", self.latitude, self.longitude];
    NSString *string = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?category=%@&start=0&count=20&%@", arr[button.tag - 100], str];
    NSLog(@"%@",string);
    
    [manager GET:string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSArray *arr = [responseObject objectForKey:@"items"];
        for (NSDictionary *dic in arr) {
            
            NearbyModel *model = [[NearbyModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.ALLarr addObject:model];
        }
        NSLog(@"%ld", self.ALLarr.count);
        [self.tableview reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearbyWebViewController *web = [[NearbyWebViewController alloc]init];
    
    [[self.ALLarr objectAtIndex:indexPath.row] slug_url];
    
    [self.navigationController pushViewController:web animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
